package com.cloudbroker.bcs.csvc.sms.hsiuc.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.cloudbroker.bcs.csvc.itn.api.constants.ITNFields;
import com.cloudbroker.bcs.csvc.itn.api.service.ITNService;
import com.cloudbroker.bcs.csvc.itn.api.t2service.ITNT2Service;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.csvc.sms.api.constants.SMSFields;
import com.cloudbroker.bcs.csvc.sms.api.constants.SMSImplTypes;
import com.cloudbroker.bcs.csvc.sms.common.service.SMSServiceAbstractImpl;
import com.cloudbroker.bcs.csvc.sms.common.util.SMSServiceHelper;
import com.cloudbroker.bcs.csvc.sms.hsiuc.constants.HSIUCFields;

@Service(SMSImplTypes.HSIUC_T2)
@Lazy
public class SMSServiceHSIUCT2Impl extends SMSServiceAbstractImpl {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(SMSServiceHSIUCHttpImpl.class);
    
    @Resource
    private ITNT2Service itnServiceT2;
    
    @Override
    public void sendSingle(Map<String, Object> param, Map<String, Object> result) {
        Map<String,Object> map = getSendMessageParam(param);
        String mobileTel = ValueUtil.getString(map.get(SMSFields.MOBILE_TEL));
        try {
            Map<String, Object> jresResult = itnServiceT2.sendSms(map);
            collectResult(jresResult , result);
            if( !VOUtil.isSuccess(jresResult) ) {
                LOGGER.error("send to " + mobileTel + " error");
            }
        } catch (Exception e) {
            LOGGER.error("send single sms failed", e);
            SMSServiceHelper.markCommonError(result);
        }
    }

    @Override
    public void sendMail(Map<String, Object> param, Map<String, Object> result) {
        Map<String,Object> map = getSendMailParam(param);
        String emailTo = ValueUtil.getString(map.get(SMSFields.EMAIL_TO));
        try {
            Map<String, Object> jresResult = itnServiceT2.sendMail(map);
            collectResult(jresResult , result);
            if( !VOUtil.isSuccess(jresResult) ) {
                LOGGER.error("send to " + emailTo + " error");
            }
        } catch (Exception e) {
            LOGGER.error("send mail failed", e);
            SMSServiceHelper.markCommonError(result);
        }
    }

    private Map<String,Object> getCommonParam(Map<String,Object> param) {
        Map<String,Object> map = new HashMap<>();
        String ownCompanyId = SMSServiceHelper.removeParamRequiredAsString(param, SMSFields.OWN_COMPANY_ID);
        String companyId = SMSServiceHelper.removeParamRequiredAsString(param, SMSFields.COMPANY_ID);
        String businsysNo = SMSServiceHelper.removeParamRequiredAsString(param, SMSFields.BUSINSYS_NO);
        map.put(ITNFields.OWN_COMPANY_ID,ownCompanyId);
        map.put(ITNFields.COMPANY_ID,companyId);
        map.put(ITNFields.BUSINSYS_NO,businsysNo);
        return map;
    }

    private Map<String,Object> getSendMailParam(Map<String, Object> param) {
        Map<String,Object> map = getCommonParam(param);
        String emailTo = SMSServiceHelper.removeParamRequiredAsString(param, SMSFields.EMAIL_TO);
        String emailCc = SMSServiceHelper.removeParamWithoutRequired(param, SMSFields.EMAIL_CC);
        String emailBcc = SMSServiceHelper.removeParamWithoutRequired(param, SMSFields.EMAIL_BCC);
        String emailSubject = SMSServiceHelper.removeParamWithoutRequired(param, SMSFields.EMAIL_SUBJECT);
        String emailText = SMSServiceHelper.removeParamRequiredAsString(param, SMSFields.EMAIL_TEXT);
        String emailPriority = SMSServiceHelper.removeParamWithoutRequired(param, SMSFields.EMAIL_PRIORITY);
        map.put(SMSFields.EMAIL_TO,emailTo);
        map.put(SMSFields.EMAIL_TEXT , emailText);
        if( emailCc != null )   map.put(SMSFields.EMAIL_CC, emailCc);
        if( emailBcc != null )  map.put(SMSFields.EMAIL_BCC, emailBcc);
        if( emailSubject != null )  map.put(SMSFields.EMAIL_SUBJECT , emailSubject);
        if( emailPriority != null ) map.put(SMSFields.EMAIL_PRIORITY , emailPriority);
        return map;
    }

    private Map<String,Object> getSendMessageParam(Map<String,Object> param) {
        Map<String,Object> map = getCommonParam(param);
        int smsPrioriry = ValueUtil.getInt(SMSServiceHelper.removeParamRequired(param, SMSFields.SMS_PRIORITY), 4);
        String mobileTel = SMSServiceHelper.getParamRequiredAsString(param, SMSFields.MOBILE_TEL);
        String content = SMSServiceHelper.getParamRequiredAsString(param, SMSFields.SMS_CONTENT);
        String auth_key = String.valueOf(param.get(SMSFields.SMS_AUTHKEY));
        map.put(ITNFields.SMS_PRIORITY,smsPrioriry);
        map.put(ITNFields.MOBILE_TEL,mobileTel);
        map.put(ITNFields.SMS_CONTENT,content);
        if(StringUtils.isNotBlank(auth_key)){
            map.put(ITNFields.SMS_AUTHKEY,auth_key);
            LOGGER.info(ITNFields.SMS_AUTHKEY + " ====> " + auth_key);
        }
        return map;
    }

    private void collectResult(Map<String,Object> jresResult , Map<String,Object> result) {
        if (VOUtil.isSuccess(jresResult)) {
            result.put(SMSFields.SEND_CHN_MSG_ID, result.get(HSIUCFields.SMS_ID));
            SMSServiceHelper.markSuccess(result);
        } else {
            String errorCode = ValueUtil.getString(result.get(HSIUCFields.ERROR_CODE));
            String errorInfo = ValueUtil.getString(result.get(HSIUCFields.ERROR_INFO));
            SMSServiceHelper.markRemoteServiceBizError(result, "send failed", errorCode, errorInfo);
        }
    }

    private void sendSingleInner(Map<String, Object> param, Map<String, Object> result) {

    }
}
